<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">G优旺管理系统</h3>
      <el-form-item prop="storeUserPhone">
        <el-input
          v-model="loginForm.storeUserPhone"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-form-item prop="storeUserPwd">
        <el-input
          v-model="loginForm.storeUserPwd"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type"  :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
<!--    <div class="el-login-footer">-->
<!--      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>-->
<!--    </div>-->
  </div>
</template>

<script>
// import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import { login, registerTravelUser } from '@/api/login'

export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      loginForm: {
        storeUserPhone: '',
        storeUserPwd: '',
        logType: 1,
        ip: '',
        rememberMe: false,
      },
      loginRules: {
        storeUserPhone: [
          { required: true, trigger: 'blur', message: '请输入您的账号' }
        ],
        storeUserPwd: [
          { required: true, trigger: 'blur', message: '请输入您的密码' }
        ]
        // code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: true,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCookie()
  },
  methods: {
    getCookie() {
      const storeUserPhone = Cookies.get('storeUserPhone')
      const storeUserPwd = Cookies.get('storeUserPwd')
      const rememberMe = Cookies.get('rememberMe')
      const logType = Cookies.get('logType')
      const ip = Cookies.get('ip')
      this.loginForm = {
        storeUserPhone: storeUserPhone === undefined ? this.loginForm.storeUserPhone : storeUserPhone,
        storeUserPwd: storeUserPwd === undefined ? this.loginForm.storeUserPwd : decrypt(storeUserPwd),
        logType: logType === undefined ? this.loginForm.logType : logType,
        ip: ip === undefined ? this.loginForm.ip : ip,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('userPhone', this.loginForm.storeUserPhone, { expires: 50 })
            Cookies.set('userPassword', encrypt(this.loginForm.storeUserPwd), { expires: 50 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove('storeUserPhone')
            Cookies.remove('storeUserPwd')
            Cookies.remove('rememberMe')
          }
          console.log(this.loginForm,"测试")
          // let body={
          //   userPhone:this.loginForm.storeUserPhone,
          //   userPassword:this.loginForm.storeUserPwd,
          // }
          // login({ body }).then(res => {
          //   this.$store.commit('updateUserInfo', res.body);
          //   this.$message.success('登录成功')
          //   this.$router.push('/index')
          //
          // })
          this.$store.dispatch('Login', this.loginForm).then((res) => {

            this.$router.push({ path: this.redirect || '/' }).catch(() => {
            })
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: right;
  padding-right:10%;
  align-items: center;
  height: 100%;
  background: url("../assets/images/bj.jpg") center center no-repeat;
  background-size: cover;
}

.title {
  margin: 0 auto 30px auto;
  text-align: center;
  color: #000000;
}

.login-form {
  border-radius: 6px;
  background: hsla(0, 0%, 100%, 1);
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
